package com.wocai.jrt.orgadmin.employee.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vteba.web.action.GenericAction;
import com.vteba.web.action.JsonBean;
import com.wocai.jrt.orgadmin.employee.bean.EmpGroupTreeBean;
import com.wocai.jrt.orgadmin.employee.model.Employee;
import com.wocai.jrt.orgadmin.employee.model.EmployeeGroup;
import com.wocai.jrt.orgadmin.employee.service.spi.EmployeeGroupService;
import com.wocai.jrt.orgadmin.employee.service.spi.EmployeeService;
import com.wocai.jrt.orgadmin.riskEvaluation.service.spi.QuestionService;
import com.wocai.jrt.orgadmin.utils.UserUtils;

/**
 * 理财团队控制器
 * 
 * @author yinlei
 * @date 2015-8-4 15:49:29
 */
@Controller
@RequestMapping("/groupEmployee")
public class GroupEmployeeAction extends GenericAction<EmployeeGroup> {
	private static final Logger LOGGER = LoggerFactory.getLogger(GroupEmployeeAction.class);

	@Inject
	private EmployeeGroupService employeeGroupServiceImpl;
	@Inject
	private EmployeeService employeeServiceImpl;
	@Inject
	private QuestionService QuestionServiceImpl;

	@RequestMapping("/eggs")
	public String eggs() {
		return "common/eggs";
	}

	/**
	 * 更新旧数据
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/empGroupScript")
	public JsonBean empGroupScript(String key) {
		JsonBean json = new JsonBean();
		if (null == key) {
			json.setCode(0);
			json.setMessage("你要干嘛");
			return json;
		}
		int i = 0;
		if (key.equals("gegebingfeilangdexuming")) {
			i = employeeGroupServiceImpl.empGroupScript();
			if (i == 1) {
				json.setCode(1);
				json.setMessage("可以可以");
				return json;
			} else {
				json.setCode(1);
				json.setMessage("你想多了");
				return json;
			}
		} else if (key.equals("renrendoushichanpingjinli")) {
			i = employeeGroupServiceImpl.investorScript();
			if (i == 1) {
				json.setCode(1);
				json.setMessage("厉害啊");
				return json;
			} else {
				json.setCode(1);
				json.setMessage("你想多了");
				return json;
			}
		}else  if(key.equals("riskdisclosurepdfcreate")){
			i = QuestionServiceImpl.investorScript();
			if (i >= 1) {
				json.setCode(1);
				json.setMessage("厉害啊:一共更新了"+i+"条数据");
				return json;
			} else {
				json.setCode(1);
				json.setMessage("已经全部更新完了");
				return json;
			}
		}else if (key.equals("weismyaozheyangduiwomeng")) {
			//更新IM的东西和inverter与customer关联关系
			i = employeeGroupServiceImpl.imEggs();
			if (i >= 1) {
				json.setCode(1);
				json.setMessage("厉害啊:一共更新了"+"很多"+"条数据");
				return json;
			} else {
				json.setCode(1);
				json.setMessage("已经全部更新完了");
				return json;
			}
		} else if (key.equals("gengxinfaxingfangshezhi")) {
			//更新IM的东西和inverter与customer关联关系
			i = employeeGroupServiceImpl.saveRevisitEggs();
			json.setCode(1);
			json.setMessage("厉害啊:一共更新了"+i+"个机构");
			return json;
		} else {
			json.setCode(0);
			json.setMessage("你想干嘛");
			return json;
		}
	}

	@RequestMapping("/list")
	public String list(EmployeeGroup model, Map<String, Object> maps) {
		return "groupEmployee/list";
	}

	@ResponseBody
	@RequestMapping("/tree")
	public EmpGroupTreeBean tree() {
		EmpGroupTreeBean empGroupTreeBean = null;
		try {
			empGroupTreeBean = employeeGroupServiceImpl.tree(UserUtils.getOrgId());
		} catch (Exception e) {
			LOGGER.error("get resource tree error, errorMsg=[{}].", e.getMessage());
		}
		return empGroupTreeBean;
	}

	@ResponseBody
	@RequestMapping("/treeEmp/{groupId}")
	public EmpGroupTreeBean treeEmp(@PathVariable("groupId") String groupId) {
		EmpGroupTreeBean empGroupTreeBean = null;
		try {
			empGroupTreeBean = employeeGroupServiceImpl.treeEmp(UserUtils.getOrgId(), groupId);
		} catch (Exception e) {
			LOGGER.error("get resource tree error, errorMsg=[{}].", e.getMessage());
		}
		return empGroupTreeBean;
	}

	/**
	 * 根据Id获得理财团队实体，Json格式。
	 * 
	 * @param id
	 *            参数id
	 * @return 理财团队实体
	 */
	@ResponseBody
	@RequestMapping("/getGroupEmploee")
	public Map<String, Object> getGroupEmploee(@RequestBody Employee bean) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			if (null == bean.getDeptId()) {
				bean.setDeptId(UserUtils.getOrgId());
			}
			bean.setState(1);
			bean.setOrderBy("employee_no asc");
			List<Employee> list = employeeServiceImpl.queryList(bean);
			for (Employee employee : list) {
				employee.setLoginPwd(null);
				employee.setWorkPwd(null);
			}
			bean.setStart(null);
			long totalRecord = employeeServiceImpl.countBy(bean);
			result.put("aaData", list); // 返回的数据
			result.put("iTotalRecords", totalRecord); // 总记录数
			result.put("iTotalDisplayRecords", totalRecord); // 显示的记录数
		} catch (Exception e) {

			LOGGER.error("get record error, errorMsg=[{}].", e.getMessage());
		}
		return result;
	}
}
